<?php

namespace app\job;

/**
 * 金币分销队列
 */

use app\admin\model\Config;
use app\model\DistributionSetting;
use app\model\GoldIncrease;
use app\model\User;
use libs\Redis;
use think\Db;
use think\facade\Log;
use think\queue\Job;
use util\Logger;

class DistributionGoldJob
{
    public function fire(Job $job, $data)
    {
        if ($job->attempts() > 3) {
            $this->failed($data);
            $job->delete();
        }
        $user_id = $data['user_id'];
        $gold    = $data['gold'];
        try {
            $res               = false;
            $user              = User::where('id', $user_id)->find();
            $level_info        = DistributionSetting::getLevelInfo(1, 1);
            $distribution_type = $level_info['type'];
            //判断有没有设置分销
            if ($distribution_type > 0) {
                $ratio_one     = $level_info['ratio_one']; // 直推金币比例
                $ratio_two     = $level_info['ratio_two']; // 间推金币比例
                $one_user_gold = floor($gold * $ratio_one / 100);
                //判断有没有上级
                $one_user = User::where('token', $user['parent_token'])->find();
                //处理直推上级金币
                if ($one_user) {
                    Db::startTrans();
                    $one_gold_after = $one_user['gold'] + $one_user_gold;
                    //添加分成金币记录
                    GoldIncrease::addRecord($one_user['id'], GoldIncrease::TYPE_VIDEO_FRIEND, $one_user_gold, $one_user['gold'], $one_gold_after, GoldIncrease::TYPES[GoldIncrease::TYPE_VIDEO_FRIEND], 1, $user_id);
                    //修改上级金币
                    $one_res = User::where('id', $one_user['id'])->update([
                        'gold' => $one_gold_after,
                    ]);
                    $two_res = true;
                    if (!empty($one_user['parent_token']) && $distribution_type == 2) {
                        $two_user       = User::where('token', $one_user['parent_token'])->find();
                        $two_user_gold  = floor($gold * $ratio_two / 100);
                        $two_gold_after = $two_user['gold'] + $two_user_gold;
                        //添加分成金币记录
                        GoldIncrease::addRecord($two_user['id'], GoldIncrease::TYPE_VIDEO_FRIEND, $two_user_gold, $two_user['gold'], $two_gold_after, GoldIncrease::TYPES[GoldIncrease::TYPE_VIDEO_FRIEND], 1, $user_id);
                        //修改上级金币
                        $two_res = User::where('id', $two_user['id'])->update([
                            'gold' => $two_gold_after,
                        ]);
                    }
                    if ($one_res && $two_res) {
                        Db::commit();
                        $res = true;
                    } else {
                        Db::rollback();
                    }
                }
            } else {
                $res = true;
            }
            if ($res) {
                $job->delete();
            }
        } catch (\Exception $e) {
            Logger::error('金币分销失败', [
                'data' => $data,
                'msg'  => $e->getMessage(),
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
        $job->delete();
    }

    public function failed($data)
    {
        Log::init([
            'type' => 'file',
            'path' => app()->getRootPath() . 'runtime/queue/',
        ]);
        $log_data = [
            'msg'  => '金币分销尝试已达最大次数',
            'data' => $data,
        ];
        Log::error($log_data);
    }
}
